package com.wendyxie.sproutup.view.widget

import android.content.Context
import android.graphics.Color
import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import android.text.method.NumberKeyListener
import android.util.AttributeSet
import android.view.*
import android.widget.EditText
import android.widget.LinearLayout
import android.widget.Toast
import androidx.annotation.ColorInt
import com.wendyxie.sproutup.R
import com.wendyxie.sproutup.utils.DimensionUtils


class PwdEditLinearLayout(context: Context?, attrs: AttributeSet?) : LinearLayout(context, attrs) {

    var onTextCompletedListener:OnTextCompletedListener ? = null
    // 定义待实现的监听器
    abstract class OnTextCompletedListener {
        abstract fun onTextCompleted(text: String)
    }

    // <!-- 默认间距 -->
    private var mDefaultSpacing = 16f;

    // <!-- 默认颜色 -->
    @ColorInt
    private var mDefaultColor = Color.BLACK;

    // <!-- 默认字体大小 -->
    private var mDefaultTextSize = 36f;

    // <!-- 当前输入位分割线颜色 -->
    @ColorInt
    private var mCurrentSplitLineColor = mDefaultColor;

    // <!-- 其他输入位分割线颜色 -->
    @ColorInt
    private var mOtherSplitLineColor = mDefaultColor;

    // <!-- 分割线高度 -->
    private var mSplitLineHeight = 1;

    // <!-- 验证码位数 -->
    private var mDigit = 4;

    // <!-- 单个验证码宽度 -->
    private var mSingleCaptchaWidth = 100;

    // <!-- 验证码当前输入位字体颜色 -->
    @ColorInt
    private var mCurrentTextColor = mDefaultColor;

    // <!-- 验证码当前输入位字体大小 -->
    private var mCurrentTextSize = mDefaultTextSize;

    // <!-- 验证码其他输入位字体颜色 -->
    @ColorInt
    private var mOtherTextColor = mDefaultColor;

    // <!-- 验证码其它输入位字体大小 -->
    private var mOtherTextSize = mDefaultTextSize;

    // 记录当前输入文本
    private var mText = "";

    private var editTextList = mutableListOf<EditText>()

    init {
        orientation = HORIZONTAL
        gravity = Gravity.CENTER
        if (childCount > 0) {
            removeAllViews()
        }
        initAttrs(context!!, attrs)
        if (mDigit <= 0) {
            mDigit = 4
        }
        // 单个验证码
        for (i in 0 until mDigit) {
            initItemView(i)
        }

        editTextList[0].requestFocus()
    }

    // 设置（未）选中属性
    private fun initItemView(index: Int) {
        // 实例化 ITEM 组件
        val child = LayoutInflater.from(context).inflate(R.layout.view_pwd_edit_text_item, this, false)
        val lp = LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT, 1f)
        if (index != 0) {
            lp.leftMargin = DimensionUtils.dp2px(context, mDefaultSpacing)
        }
        child.layoutParams = lp

        val numEditText = child.findViewById<EditText>(R.id.number_tv);
        editTextList.add(numEditText)
        addView(child)

        numEditText.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
            }

            override fun onTextChanged(s: CharSequence, p1: Int, p2: Int, p3: Int) {
            }

            override fun afterTextChanged(s: Editable) {
                // 输入操作
                if (s.isEmpty() && index > 0) {
                    // 删除操作
                    editTextList[index - 1].requestFocus() // 跳转到上一个输入框
                } else if (s.length == 1 && index < mDigit - 1) {
                    editTextList[index + 1].requestFocus() // 跳转到下一个输入框
                } else if (s.length == 1 && index == mDigit - 1) {
                    // 验证密码
                    val pwd = getText()
                    onTextCompletedListener?.onTextCompleted(pwd)
                }
            }
        })
    }

    private fun initAttrs(context: Context, attrs: AttributeSet?) {
        if (attrs != null) {
            // AttributeSet 属性值的索引
            var o = context.obtainStyledAttributes(attrs, R.styleable.PwdEditText, 0, 0)
            // 默认间距
            mDefaultSpacing = o.getDimension(R.styleable.PwdEditText_defaultSpacing, 16f);
            // 获取默认颜色
            mDefaultColor = o.getColor(R.styleable.PwdEditText_defaultColor, Color.BLACK);
            // 获取默认字体大小
            mDefaultTextSize = o.getDimension(R.styleable.PwdEditText_defaultTextSize, 36f);
            // 输入位分割线颜色
            mCurrentSplitLineColor = o.getColor(R.styleable.PwdEditText_currentSplitLineColor, mDefaultColor);
            // 其他输入位分割线颜色
            mOtherSplitLineColor = o.getColor(R.styleable.PwdEditText_otherSplitLineColor, mDefaultColor);
            // 分割线高度
            mSplitLineHeight = o.getDimension(R.styleable.PwdEditText_splitLineHeight, 1f).toInt()
            if (mSplitLineHeight <= 1) {
                mSplitLineHeight = 1
            }
            // 验证码位数
            mDigit = o.getInteger(R.styleable.PwdEditText_digit, 4);
            // 单个验证码宽度
            mSingleCaptchaWidth = o.getDimension(R.styleable.PwdEditText_singleCaptchaWidth, 100f).toInt()
            // 验证码当前输入位字体颜色
            mCurrentTextColor = o.getColor(R.styleable.PwdEditText_currentTextColor, mDefaultColor);
            // 验证码当前输入位字体大小
            mCurrentTextSize = o.getDimension(R.styleable.PwdEditText_currentTextSize, mDefaultTextSize);
            // 验证码其他输入位字体颜色
            mOtherTextColor = o.getColor(R.styleable.PwdEditText_otherTextColor, mDefaultColor);
            // 验证码其它输入位字体大小
            mOtherTextSize = o.getDimension(R.styleable.PwdEditText_otherTextSize, mDefaultTextSize);
            // 回收资源
            o.recycle();
        }
    }

    // 追加文本
    fun addText(text: String) {
        var text = text
        text = if (TextUtils.isEmpty(text)) "" else text
//        setText(mText + text)
    }

    // 删除文本
    fun delText() {
        val count = if (TextUtils.isEmpty(mText)) 0 else mText.length
//        if (count > 0) {
//            setText(mText.substring(0, count - 1))
//        } else {
//            setText("")
//        }
    }

    // 获取文本
    fun getText(): String {
        var text = ""
        for (editText in editTextList) {
            text += editText.text.toString()
        }
        return text
    }
}
